﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using CommunityToolkit.Mvvm.ComponentModel;
using Macad.Occt;
using SPCAM.Plugin.Cam.Model;
using SPCAM.Plugin.Cam.Servers;
using Macad.Core;
using SpCad;
using Macad.Interaction;
using static System.ComponentModel.Design.ObjectSelectorEditor;
using Macad.Common;
using Macad.Core.Topology;
using System.Reflection;
using Macad.Core.Shapes;
namespace SPCAM.Plugin.Cam.ViewModel
{
    public class WeldTreeViewModel : ObservableObject
    {
      
        public ObservableCollection<WeldTreeModel> TreeNode => WeldServer.Get.TreeNode;
        private List<EdgePropertyModel> _EdgeModel;
        public WeldTreeViewModel()
        {
            Shape.ShapeChanged += Shape_ShapeChanged;
        }

        private async void Shape_ShapeChanged(Shape shape)
        {
            await Task.Delay(10);
            _UpdateAisShape();
        }


        /// <summary>
        /// 边框的属性用于显示边框的参数
        /// </summary>
        public List<EdgePropertyModel> EdgeModels
        {
            get => _EdgeModel;
            set => SetProperty(ref _EdgeModel, value);

        }
        private WeldTreeModel _SelectWeld;
        public WeldTreeModel SelectWeld
        {
            get => _SelectWeld;
            set
            {

                SetProperty(ref _SelectWeld, value);
                _UpdateAisShape();
                if (value != null)
                {

                    EdgeModels = value.EdgePropertyModels;
                    var AisShape = new AIS_Shape(value.TopoDSEdge);
                    AisShape.SetColor(Colors.FilteredSubshapesHot.ToQuantityColor());
                    // SpServer.Instance.Current.WorkspaceController.sel
                    // InteractiveContext.Current.WorkspaceController?.Selection.SelectEntities(AisShape);
                }

            }
        }
        #region Visualization
        AIS_Shape _AisShape;
        void _UpdateAisShape()
        {
            if (SelectWeld == null) { return; }
            //TODO 此处为高亮显示
            try
            {

                if (_AisShape != null)
                {
                    // Remove AIS Shape

                    SpServer.Instance.WorkspaceController.Workspace.AisContext.Remove(_AisShape, false);
                    SpServer.Instance.WorkspaceController.Workspace.AisContext.Erase(_AisShape, false);
                    _AisShape = null;
                }

                var ocShape = (TopoDS_Shape)SelectWeld?.TopoDSEdge;
                var newSelection = SelectWeld?.TBody;
                var trsf = SelectWeld?._trsf;

                if (ocShape != null )
                {
                    // Init AIS Shape
                    _AisShape = SelectWeld.AIS_Shape;
                    if( trsf != null)
                    {
                        _AisShape.SetLocalTransformation(trsf.Value);
                    }
               
                    _AisShape.SetZLayer(-2); // Graphic3d_ZLayerId_Top
                    _AisShape.SetColor(new Color(0.0f, 0.0f, 1.0f).ToQuantityColor());
                    _AisShape.Attributes().LineAspect().SetColor(new Color(0.0f, 0.0f, 1.0f).ToQuantityColor());
                    _AisShape.SetWidth(AppConfig.Get.SelctEdgeWidth);
                    _AisShape.Attributes().PointAspect().SetScale(3);

                    SpServer.Instance.WorkspaceController.Workspace.AisContext.Display(_AisShape, false);
                    SpServer.Instance.WorkspaceController.Workspace.AisContext.Redisplay(_AisShape, false, true);
                    SpServer.Instance.WorkspaceController.Workspace.AisContext.Deactivate(_AisShape);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            SpServer.Instance.WorkspaceController?.Invalidate();
        }

        //--------------------------------------------------------------------------------------------------


        #endregion

    }
}
